package api

import (
	"api/basic/global"
	"api/basic/pkg"
	__ "api/basic/proto"
	"api/handler/request"
	"fmt"
	"github.com/gin-gonic/gin"
	"log"
	"net/http"
	"path/filepath"
	"time"
)

func List(c *gin.Context) {
	var data request.GoodList
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(http.StatusOK, &gin.H{
			"code": 10001,
			"msg":  "错误",
			"data": err.Error(),
		})
		return
	}
	r, err := global.S.GoodList(c, &__.GoodListReq{Page: int64(data.Page), Size: int64(data.Size)})
	if err != nil {
		log.Fatalf("could not greet: %v", err)
	}
	log.Printf("Greeting: %s", r)
	c.JSON(http.StatusOK, &gin.H{
		"code": 200,
		"msg":  "商品展示成功",
		"data": r,
	})
}

func Upload(c *gin.Context) {
	// 单文件
	file, _ := c.FormFile("file")
	log.Println(file.Filename)
	ext := filepath.Ext(file.Filename)
	name := time.Now().Format("20060102150405") + ext
	dst := "D:\\gocode\\src\\gao4\\4\\rk\\11\\good-list\\api\\basic\\serce\\global"
	// 上传文件至指定的完整文件路径
	c.SaveUploadedFile(file, dst)
	pkg.Oss(dst, name)
	c.String(http.StatusOK, fmt.Sprintf("'%s' uploaded!", file.Filename))
	c.JSON(http.StatusOK, &gin.H{
		"code": 200,
		"msg":  "上传成功",
		"data": pkg.OssFile(name),
	})
	r, err := global.S.GoodOss(c, &__.GoodOssReq{
		Id:  1,
		Oss: name,
	})
	if err != nil {
		log.Fatalf("could not greet: %v", err)
	}
	log.Printf("Greeting: %s", r)
}

func ListRecommend(c *gin.Context) {

	r, err := global.S.GoodListRecommend(c, &__.GoodListRecommendReq{})
	if err != nil {
		log.Fatalf("could not greet: %v", err)
	}
	log.Printf("Greeting: %s", r)
	c.JSON(http.StatusOK, &gin.H{
		"code": 200,
		"msg":  "商品推荐展示成功",
		"data": r,
	})
}

func ListOne(c *gin.Context) {
	var data request.GoodListOne
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(http.StatusOK, &gin.H{
			"code": 10001,
			"msg":  "错误",
			"data": err.Error(),
		})
		return
	}
	r, err := global.S.GoodListOne(c, &__.GoodListOneReq{
		Name: data.Name})
	if err != nil {
		log.Fatalf("could not greet: %v", err)
	}
	log.Printf("Greeting: %s", r)
	c.JSON(http.StatusOK, &gin.H{
		"code": 200,
		"msg":  "商品详情成功",
		"data": r,
	})
}
